package dao

import (
	"fmt"
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type LouPan struct{}

func (l *LouPan) LikeName(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if name == "" {
			return db
		} else {
			return db.Where("`hft_loupans`.`name` LIKE ?", fmt.Sprintf("%%%v%%", name))
		}
	}
}

func (l *LouPan) GetLouPanList(link string, paginate *request.LouPaPaginate, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(l.LikeName(paginate.Name, db)).Scopes(l.JustRegion(link, paginate.Region, db)).Scopes(l.JustAudit(paginate.IsAudit, db)).Scopes(l.JustGrounding(paginate.Grounding, db)).Scopes(l.JustStatus(paginate.Status, db)).Scopes(l.LouPanPreload(db)).Scopes(Paginate(Page(paginate.Page), db)).Scopes(BaseDao.CreateTime(db, paginate.CreateTime))
	}
}

func (l *LouPan) JustRegion(link string, region int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if region == 0 {
			return db.Where("`hft_loupans`.`region_link` LIKE ? ", link)
		} else {
			return db.Where("`hft_loupans`.`region` = ?", region)
		}
	}
}

func (l *LouPan) JustGrounding(grounding int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {

	fmt.Println(grounding)
	return func(db *gorm.DB) *gorm.DB {
		if grounding == 0 {
			return db
		} else {
			return db.Where("`hft_loupans`.`grounding` = ?", grounding)
		}
	}
}

func (l *LouPan) JustAudit(isAudit int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if isAudit == 0 {
			return db
		} else {
			return db.Where("`hft_loupans`.`is_audit` = ?", isAudit)
		}
	}
}

func (l *LouPan) JustStatus(status int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if status == 0 {
			return db
		} else {
			return db.Where("`hft_loupans`.`status` = ?", status)
		}
	}
}

func (l *LouPan) LouPanPreload(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("SellStatus").Preload("SellLouPanInfo").Preload("Properties").Preload("Tags").Preload("RoomType").Preload("Disadvantage").Preload("LouPanArea").Preload("TeSe").Order("sort desc, create_time desc")
	}
}

// ----------------------------获取新楼盘------------------------------
func (l *LouPan) NewSelect(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Select("hft_loupans.`min_area`,hft_loupans.`max_area`,`hft_loupans`.`id`,SUM( CASE WHEN `hft_account_lou_pan`.`lou_pan_id` IS NULL THEN 0 ELSE 1 END ) as garrison,`hft_account_lou_pan`.`lou_pan_id`,`hft_loupans`.`create_time`,`hft_loupans`.`grounding`,`hft_loupans`.`reference_price`,`hft_loupans`.`deleted_at`, `hft_loupans`.`sort`,`hft_loupans`.`name`,`hft_loupans`.`cover`,`hft_loupans`.`region_link`,`hft_loupans`.`region`,`hft_loupans`.`belong_name`,`hft_loupans`.`status`,`hft_loupans`.`active_last_update`")
	}
}
func (l *LouPan) PreloadLouPan(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("SellStatus")
	}
}
func (l *LouPan) NewOrder(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Order("`hft_loupans`.`sort` desc")
	}
}
func (l *LouPan) NewJoin(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.
			Joins("LEFT JOIN `hft_account_lou_pan` ON `hft_loupans`.`id` = `hft_account_lou_pan`.`lou_pan_id`")
	}
}
func (l *LouPan) GetNewLouPanList(link string, paginate *request.LouPaPaginate, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Preload("SellLouPanInfo").
			Scopes(l.NewSelect(db)).
			Scopes(l.LikeName(paginate.Name, db)).
			Scopes(l.JustRegion(link, paginate.Region, db)).
			Scopes(l.JustAudit(paginate.IsAudit, db)).
			Scopes(l.JustGrounding(paginate.Grounding, db)).
			Scopes(l.JustStatus(paginate.Status, db)).
			Scopes(l.PreloadLouPan(db)).
			Scopes(l.NewJoin(db)).
			Scopes(l.NewOrder(db)).
			Group("`hft_loupans`.`id`").
			Scopes(Paginate(Page(paginate.Page), db)).
			Scopes(BaseDao.CreateTime(db, paginate.CreateTime))
	}
}

var LouPanDao = new(LouPan)
